package com.encrypt;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.GeneralSecurityException;

/**
 * Created by 欢 on 2016/10/22.
 */
public class SHA1Coder {

    public static final String ALGORITHM = "PBKDF2WithHmacSHA1";
    public static final int ITERATION_COUNT = 8192;
    public static final int KEY_SIZE = 160;

    public static byte[] encrypt(char[] password, byte[] salt)
            throws GeneralSecurityException {
        return encrypt(password, salt, ITERATION_COUNT, KEY_SIZE);
    }

    public static byte[] encrypt(char[] password, byte[] salt,
                                 int iterationCount, int keySize) throws GeneralSecurityException {
        if(salt == null)
            return null;
        PBEKeySpec spec = new PBEKeySpec(password, salt, iterationCount, keySize);
        SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
        return factory.generateSecret(spec).getEncoded();
    }

}
